import moment from 'moment'
//type为字符串类型，有两种选择，"s"代表开始,"e"代表结束，dates为数字类型，不传或0代表本周，-1代表上周，1代表下周 k为指定周的周几
export function getMonday(type, dates, k) {
	var now = new Date();
	var nowTime = now.getTime();
	var day = now.getDay();
	var longTime = 24 * 60 * 60 * 1000;
	var n = longTime * (7 * (dates || 0) + k);
	if (type == "s") {
		var dd = nowTime - (day - 1) * longTime + n;
	};
	if (type == "e") {
		var dd = nowTime + (7 - day) * longTime + n;
	};
	dd = new Date(dd);
	var y = dd.getFullYear();
	var m = dd.getMonth() + 1;
	var d = dd.getDate();
	m = m < 10 ? "0" + m : m;
	d = d < 10 ? "0" + d : d;
	var day = y + "-" + m + "-" + d;
	return day;
};
//dates为数字类型，0代表今日,-1代表昨日，1代表明日
export function getDate(dates) {
	var dd = new Date();
	var n = dates || 0;
	dd.setDate(dd.getDate() + n);
	var y = dd.getFullYear();
	var m = dd.getMonth() + 1;
	var d = dd.getDate();
	m = m < 10 ? "0" + m : m;
	d = d < 10 ? "0" + d : d;
	var day = y + "-" + m + "-" + d;
	return day;
};

//根据周几的数字获取昨天周几的数字
export function getLastDadyDay(num) {
	if (num == 0) {
		return 6;
	} else {
		return num - 1;
	}
}

//将中国标准时间转为指定时间规范
export function changeChineToDate(date, format) {
	// date.setHours(date.getHours() - 14);
	// var o = {
	// 	'M+': date.getMonth() + 1, //month
	// 	'd+': date.getDate(), //day
	// 	'H+': date.getHours(), //hour
	// 	'm+': date.getMinutes(), //minute
	// 	's+': date.getSeconds(), //second
	// 	'q+': Math.floor((date.getMonth() + 3) / 3), //quarter
	// 	'S': date.getMilliseconds() //millisecond
	// };

	// if (/(y+)/.test(format))
	// 	format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));

	// for (var k in o)
	// 	if (new RegExp('(' + k + ')').test(format)) {
	// 		format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k])
	// 		.length));
	// 	}
	// return format;
	return moment(date).format(format)
}
//获取几天后的日期
export function fun_date(aa) {
	var date1 = new Date(),
		time1 = date1.getFullYear() + "-" + (date1.getMonth() + 1) + "-" + date1.getDate(); //time1表示当前时间  
	var date2 = new Date(date1);
	date2.setDate(date1.getDate() + aa);
	var y = date2.getFullYear();
	var m = date2.getMonth() + 1;
	var d = date2.getDate();
	m = m < 10 ? "0" + m : m;
	d = d < 10 ? "0" + d : d;
	var day = y + "-" + m + "-" + d;
	return day;
}

//通过 年月日判断是不是周末
export function checkFreeDayByDateStr(dateStr) {
	var date = new Date(dateStr);
	if (date.getDay() % 7 == 6 || date.getDay() % 6 == 0) {
		return true;
	}
	return false;
}

export function IdCard(UUserCard, num) {
	if (num == 1) {
		//获取出生日期
		var birth = UUserCard.substring(6, 10) + "-" + UUserCard.substring(10, 12) + "-" + UUserCard.substring(12, 14);
		return birth;
	}
	if (num == 2) {
		//获取性别
		if (parseInt(UUserCard.substr(16, 1)) % 2 == 1) {
			//男
			return '1';
		} else {
			//女
			return "2";
		}
	}
	if (num == 3) {
		//获取年龄
		var myDate = new Date();
		var month = myDate.getMonth() + 1;
		var day = myDate.getDate();
		var age = myDate.getFullYear() - UUserCard.substring(6, 10) - 1;
		if (UUserCard.substring(10, 12) < month || UUserCard.substring(10, 12) == month && UUserCard.substring(12,
			14) <= day) {
			age++;
		}
		return age;
	}
}

export default {
	getMonday,
	checkFreeDayByDateStr,
	fun_date,
	changeChineToDate,
	getLastDadyDay,
	getDate,
	getMonday,
	IdCard
}
